package com.cakebackoffice;


import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.cakebackoffice.Model.CatalogCategory;
import com.cakebackoffice.Model.MenuType;
import com.cakebackoffice.Model.ProductType;
import com.cakebackoffice.Model.Store;


public class CatalogDisplayFragment extends Fragment {
    private LinearLayout mRootLayout;
    private LinearLayout mCategoryLayout;
    private LinearLayout mProductLayout;
    private LinearLayout mMenuLayout;

    private static Long selectedProduct;


    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRootLayout = (LinearLayout) inflater.inflate(R.layout.catalog_detail_welcome, container, false);
        mProductLayout = (LinearLayout) inflater.inflate(R.layout.catalog_detail_product, container, false);
        mCategoryLayout = (LinearLayout) inflater.inflate(R.layout.catalog_detail_category, container, false);
        mMenuLayout = (LinearLayout) inflater.inflate(R.layout.catalog_detail_menu, container, false);
        return mRootLayout;
    }


    public void displayProductDetail(ProductType product) {
        setFragmentContentView(mProductLayout);

        selectedProduct = product.getIdDB();

        TextView tvProductName = (TextView) getView().findViewById(R.id.catalog_textview_product_name);
        TextView tvProductDetail = (TextView) getView().findViewById(R.id.catalog_text_view_description);
        tvProductDetail.setOnClickListener(onProductPropertyClickListenneur);
        tvProductDetail.setTag(CatalogPropertyEnum.DESCRIPTION);
        TextView tvRecommendedPrice = (TextView) getView().findViewById(R.id.catalog_text_view_recommended_price);
        tvRecommendedPrice.setOnClickListener(onProductPropertyClickListenneur);
        tvRecommendedPrice.setTag(CatalogPropertyEnum.RECOMMENDED_PRICE);
        TextView tvLocalPrice = (TextView) getView().findViewById(R.id.catalog_text_view_local_price);
        tvLocalPrice.setOnClickListener(onProductPropertyClickListenneur);
        tvLocalPrice.setTag(CatalogPropertyEnum.LOCAL_PRICE);
        TextView tvVATcategory = (TextView) getView().findViewById(R.id.catalog_text_view_VATDescription);

        tvProductName.setText(product.getName());
        tvProductDetail.setText(product.getDescription());
        if (product.getRecommendedPriceHashMap().get(null) != null)
            tvRecommendedPrice.setText(product.getRecommendedPriceHashMap().get(null).getValue().toString());
        tvLocalPrice.setText(product.getLocalPrice(Store.getLocalStore(getActivity().getBaseContext())).toString());
        tvVATcategory.setText("Soon");
    }

    public void displayCategory(CatalogCategory category) {
        setFragmentContentView(mCategoryLayout);
        TextView tv = (TextView) getView().findViewById(R.id.catalog_textview_catageroy_name);
        tv.setText("Category: " + category.getName());
    }

    public void displayMenu(MenuType menu) {
        setFragmentContentView(mMenuLayout);
    }


    void setFragmentContentView(View view) {
        LinearLayout.LayoutParams layoutParams =
                new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
                        ViewGroup.LayoutParams.FILL_PARENT);
        mRootLayout.removeAllViews();
        mRootLayout.addView(view, layoutParams);
    }


    // Les listenners!
    // Pour modifier une propriétée d'un product:
    private View.OnClickListener onProductPropertyClickListenneur = new View.OnClickListener() {
        public void onClick(View v) {
            ((CatalogMenuActivity) getActivity()).openDialogPropertyEdit(selectedProduct, (CatalogPropertyEnum) v.getTag());
        }
    };

    private View.OnClickListener onProductLocalPricePropertyClickListenneur = new View.OnClickListener() {
        public void onClick(View v) {


            //((CatalogMenuActivity) getActivity()).openDialogPropertyEdit();
//            getCategory(categoryIdStack.lastElement(), false);

        }
    };


}
